package com.aabte.controller;

import com.aabte.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

@RestController
@Log4j
@RequestMapping("/v1")
@Api(value = "/v1", description = "用户管理模块")
public class UserManager {

    @Autowired
    private SqlSessionTemplate template;

    @ApiOperation(value = "登陆接口", httpMethod = "POST")
    @PostMapping("/login")
    public boolean login(@RequestBody User user,
                         HttpServletRequest request,
                         HttpServletResponse response){
        log.debug("start invoke login interface");

        boolean result;

        int rs = template.selectOne("login", user);
        if (rs == 1) {
            Cookie cookie = new Cookie("login", "true");
            response.addCookie(cookie);
            log.info("登陆成功！");
            result = true;
        } else {
            result = false;
        }
        log.debug("finish invoke login interface");
        return result;
    }

    @ApiOperation(value = "添加用户接口", httpMethod = "POST")
    @PostMapping("/addUser")
    public boolean addUser(@RequestBody User user,
                           HttpServletRequest request,
                           HttpServletResponse response) {
        log.debug("start invoke addUser interface");
        boolean result;

        result = verifyCookies(request);
        if (!result) {
            return result;
        }

        int rs = template.insert("addUser", user);
        if (rs > 0) {
            result = true;
        }
        log.debug("finish invoke addUser interface");
        return result;
    }


    @ApiOperation(value = "获取用户列表接口", httpMethod = "POST")
    @PostMapping("/getUserList")
    public List<User> getUserList(
            @RequestBody User user,
            HttpServletRequest request,
            HttpServletResponse response){

        List<User> userList = null;
        boolean isLogin = verifyCookies(request);
        if (isLogin){
            userList = template.selectList("getUserList", user);
        }
        return userList;
    }

    @ApiOperation(value = "更新用户接口", httpMethod = "POST")
    @PostMapping("/updateUser")
    public int updateUser(
            @RequestBody User user,
            HttpServletRequest request,
            HttpServletResponse response){
        int result = 0;
        boolean isLogin = verifyCookies(request);
        if (isLogin) {
            result = template.update("updateUser", user);
            log.info("更新用户数：" + result);
        }
        return result;
    }


    private boolean verifyCookies(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (Objects.isNull(cookies)) {
            return false;
        }
        for (Cookie cookie :
                cookies) {
            if ("login".equals(cookie.getName())
                    && "true".equals(cookie.getValue())) {
                log.info("Cookie验证通过!");
                return true;
            }
        }
        return false;
    }

}